﻿/* Project: Side Scrolling Shooter
 * The player object class. Handles movement controls and health.
 * Last Update: 4/12/11,11:16PM
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TheFifthSpace
{
    public class Player : DrawableGameComponent
    {
        private int maxX, maxY, minX, minY; // Boundaries for player
        public float posX, posY; // Current x and y positions
        private float velX, velY; // Current velocity
        private const float vMax = 200; // Terminal velocity
        private const float vStep = 15; // Acceleration
        private float dt; // Elapsed time
        private KeyboardState k; // Current keyboard state
        public Texture2D sprite { get; set; }
        public Vector2 Position
        {
            get
            {
                return new Vector2(posX, posY);
            }
            set
            {
                posX = value.X;
                posY = value.Y;
            }
        }
        public int Health { get; set; } // Current health
        public bool IsAlive { get; set; } // Is the player alive?
        private Vector2 offset;
        public Vector2 Offset
        {
            get
            {
                return offset;
            }
            set
            {
                offset.X = value.X;
                offset.Y = value.Y;
            }
        }

        //*************************** FUNCTIONS ****************************

        // CONSTRUCTOR
        public Player(Game g, Vector2 startPos)
            : base(g)
        {
            Position = startPos;
            Health = 100; // Player begins game at full health
            IsAlive = true;
        }

        // LOAD CONTENT
        protected override void LoadContent()
        {
            sprite = Game.Content.Load<Texture2D>("cannon");
            //spriteBatch = MainGame.spriteBatch;
            // Modify screen boundaries for player offset: sprite cannot leave screen
            minX = sprite.Width / 2;
            minY = 75;
            maxX = Game.GraphicsDevice.Viewport.Width - minX;
            maxY = Game.GraphicsDevice.Viewport.Height - minY;
            offset = new Vector2(minX, sprite.Height / 2);
        }

        // DRAW
        public override void Draw(GameTime gameTime)
        {
            if (MainGame.currentState != GameState.RunningGame) return;
            // First, make sure Player is still on-screen
            if (posX < minX)
                posX = minX;
            else if (posX > maxX)
                posX = maxX;
            if (posY < minY)
                posY = minY;
            else if (posY > maxY)
                posY = maxY;

            // Then, attempt to draw Player
            MainGame.spriteBatch.Begin();
            if (IsAlive) MainGame.spriteBatch.Draw(sprite, Position - offset, Color.White);
            // Draw special sprite if player is dead
            else MainGame.spriteBatch.Draw(sprite, Position - offset, Color.Red);
            MainGame.spriteBatch.End();
        }

        // When player takes damage, remove from health
        public void Damage(int d)
        {
            Health -= d;
        }

        // UPDATE
        public override void Update(GameTime gameTime)
        {
            // Check to see if player is alive
            if (Health <= 0)
                IsAlive = false;
            else
            {
                dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
                k = Keyboard.GetState();

                // Vertical movement: restrict sprite to screen
                if (k.IsKeyDown(Keys.Up) && !k.IsKeyDown(Keys.Down) && posY > minY)
                    velY -= vStep;
                else if (k.IsKeyDown(Keys.Down) && !k.IsKeyDown(Keys.Up) && posY < maxY + 15)
                    velY += 10;
                else
                    velY *= (1 - (5* dt));

                // Horizontal movement: restrict sprite to screen
                if (k.IsKeyDown(Keys.Right) && !k.IsKeyDown(Keys.Left) && posX < maxX - 20)
                    velX += vStep;
                else if (k.IsKeyDown(Keys.Left) && !k.IsKeyDown(Keys.Right) && posX > minX)
                    velX -= vStep;
                else
                    velX *= (1 - (5 * dt));

                // Speed limits
                if (velX > vMax)            velX = vMax;
                else if (velX < -vMax)      velX = -vMax;
                if (velY > vMax)            velY = vMax;
                else if (velY < -vMax)      velY = -vMax;
                posX += velX * dt;
                posY += velY * dt;
                base.Update(gameTime);

            }

        }// END UPDATE
    }
}